package validators;

import java.util.ResourceBundle;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import utils.DomainDAORemote;
import dtos.DomainDTO;

@FacesValidator("validators.DomainDoublicateValidator")
public class DomainDublicateValidator implements Validator {
	@EJB
	DomainDAORemote ddr;

	ResourceBundle bundle = ResourceBundle.getBundle(
			"languageSettings.language", FacesContext.getCurrentInstance()
					.getViewRoot().getLocale());

	public DomainDublicateValidator() {
	}

	@Override
	public void validate(FacesContext context, UIComponent component,
			Object value) throws ValidatorException {
		System.out.println(">>  " + (String) value);
		DomainDTO tDomain = ddr.getDomainByName(((String) value).trim());
		if (tDomain != null) {
			FacesMessage message = new FacesMessage(
					bundle.getString("Validators_DomainDuplicateValidator_Error"));
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}

	}

}
